<?php
namespace app\index\controller;

use app\index\model\Customer;
use app\index\model\UsersLog;
use app\index\model\Express;
use app\index\model\Recharge;
use app\index\model\Users;
use think\admin\Controller;
use think\facade\Db;
use think\facade\Session;

class Login  extends Controller{

    /**
     * @name:登录
     * @author:Sam
     * @date:2024-09-02
     * @param:[string] param [desc]
    */
    public function index(){
        if($this->app->request->isPost()){
           
            $data = $this->_vali([
                'mobile.require'   => '登录账号不能为空!',
                'password.require' => '登录密码不能为空!',
                'password.min:6'   => '登录密码不能少于6位字符!',
            ]);

            /*! 用户信息验证 */
            $map = ['mobile' => $data['mobile'], 'deleted' => 0];
            $user = Users::where($map)->find();
            if (!$user) {
                $this->app->session->set("LoginInputSessionError", true);
                $this->error('登录账号或密码错误，请重新输入!');
            }
            $user->last_ip = get_real_ip();
            $user->save();
            if (md5($data['password'].'123456789') !== $user['password']) {
                $this->app->session->set("LoginInputSessionError", true);
                $this->error('登录账号或密码错误，请重新输入!');
            }
            $this->app->session->set('userInfo', $user->toArray());
            $this->app->session->delete("LoginInputSessionError");
            $this->app->session->set('uid', $user['id']);
            return $this->success('登录成功');
        }else{
           
            return $this->fetch();
        }
      
    }


        /**
     * @name:退出登录
     * @author:Sam
     * @date:2024-06-09
     * @param:[string] param [desc]
    */
    public function logout(){
        Session::delete('userInfo');
        Session::delete('uid');
        $this->redirect(url('index/index/index'));
    }

}